package com.yingpai.fitness.dvp_2_0.main.activity.mine;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.integral.IntegralMallActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.MyCouponBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/12/17.
 */

public class MyCouponActivity extends CommonActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int pageNumber = 1;
    private int pageSize = 10;
    private QuickAdapter<MyCouponBean.DataBean.RowsBean> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dvp_2_0_activity_my_coupon);
    }

    @Override
    protected void initView() {
        setTitle("我的优惠券");
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        quickAdapter = new QuickAdapter<MyCouponBean.DataBean.RowsBean>(this, R.layout.item_my_coupon) {
            @Override
            protected void convert(BaseAdapterHelper helper, MyCouponBean.DataBean.RowsBean item) {
                helper.setText(R.id.price,"￥"+item.getMinus()/100);
                helper.setText(R.id.available,"消费满"+item.getFull()/100+"元可用");
                helper.setText(R.id.time,item.getFromDate()+" -"+item.getEndDate());
            }
        };

        listview.setAdapter(quickAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(IntegralMallActivity.class);
            }
        });
    }

    @Override
    protected void initData() {
        pageNumber = 1;
        requestData();
    }

    private void requestData() {
        showDialog(this,null);
        HashMap<String,Object> params = new HashMap();
        params.put("pageNumber",pageNumber++);
        params.put("pageSize",pageSize);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.MY_COUPON, params, Url.MY_COUPON, new Callback<MyCouponBean>() {
            @Override
            public void onSuccess(MyCouponBean myCouponBean) {
                if(myCouponBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    if (pageNumber - 1 == 1) {
                        if(myCouponBean.getData().getMyTickets()>0){
                            empty.setVisibility(View.GONE);
                            quickAdapter.clear();
                            quickAdapter.addAll(myCouponBean.getData().getRows());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        quickAdapter.addAll(myCouponBean.getData().getRows());
                    }

                    int pagecount;
                    if (myCouponBean.getData().getMyTickets() % pageSize == 0) {
                        pagecount = myCouponBean.getData().getMyTickets() / pageSize;
                    } else {
                        pagecount = myCouponBean.getData().getMyTickets() / pageSize + 1;
                    }
                    if (pageNumber > pagecount) {
                        refreshLayout.setNoMoreData(true);
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }

                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    quickAdapter.notifyDataSetChanged();
                }else{
                    if (pageNumber - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(myCouponBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNumber - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNumber = 1;
        requestData();
    }
}
